# Neatx makefile
# - Indent with tabs only.
# - Keep files sorted; one line per file.
# - Directories in lib/ must have their own *dir variable.
# - All directories must be listed in DIRS.
# - Use autogen.sh to generate Makefile.in and configure script.

# Automake doesn't export these variables before version 1.10.
abs_top_builddir = @abs_top_builddir@
abs_top_srcdir = @abs_top_srcdir@

ACLOCAL_AMFLAGS = -I autotools
REPLACE_VARS_SED = autotools/replace_vars.sed
GEN_LOG_WRAPPER = $(top_srcdir)/autotools/gen-log-wrapper

appdir = $(pkgpythondir)/app
docdir = $(datadir)/doc/$(PACKAGE)

AM_CFLAGS = -Wall -Werror -DPKGLIBDIR=\"$(pkglibdir)\"

DIRS = \
	autotools \
	doc \
	extras \
	lib \
	lib/app \
	src \
	test \
	test/python

CLEANFILES = \
	stamp-directories \
	extras/upload \
	lib/*.py[co] \
	lib/app/*.py[co] \
	test/python/*.py[co] \
	$(REPLACE_VARS_SED) \
	$(nodist_pkgpython_PYTHON) \
	$(PYTHON_BOOTSTRAP) \
	$(LOG_WRAPPER)

DISTCLEANFILES = \
	$(dochtml)

CLEANDIRS = \
	doc/api

nodist_pkgpython_PYTHON = \
	lib/_autoconf.py

pkgpython_PYTHON = \
	lib/__init__.py \
	lib/agent.py \
	lib/auth.py \
	lib/cli.py \
	lib/config.py \
	lib/constants.py \
	lib/daemon.py \
	lib/errors.py \
	lib/node.py \
	lib/protocol.py \
	lib/serializer.py \
	lib/session.py \
	lib/utils.py

app_PYTHON = \
	lib/app/__init__.py \
	lib/app/nxdialog.py \
	lib/app/nxnode.py \
	lib/app/nxserver.py \
	lib/app/nxserver_login.py

dist_pkglib_SCRIPTS = \
	src/ttysetup

nodist_pkglib_SCRIPTS = \
	$(LOG_WRAPPER) \
	$(PYTHON_BOOTSTRAP)

pkglib_PROGRAMS = \
	src/fdcopy

dist_pkgdata_DATA = \
	extras/authorized_keys.nomachine

docrst = \
	doc/design.rst

dochtml = $(patsubst %.rst,%.html,$(docrst))

doc_DATA = $(dochtml)

dist_doc_DATA = \
	doc/neatx.conf.example \
	doc/processes.txt

PYTHON_BOOTSTRAP = \
	src/nxdialog \
	src/nxnode \
	src/nxserver \
	src/nxserver-login

LOG_WRAPPER = \
	src/nxnode-wrapper \
	src/nxserver-login-wrapper

EXTRA_DIST = \
	pylintrc \
	autotools/gen-log-wrapper \
	doc/DEVNOTES \
	extras/upload.in \
	$(docrst) \
	$(dist_TESTS) \
	$(TEST_FILES)

TEST_FILES = \
	test/python/mocks.py

dist_TESTS = \
	test/python/neatx.app.nxserver_login_test.py \
	test/python/neatx.app.nxserver_test.py \
	test/python/neatx.auth_test.py \
	test/python/neatx.daemon_test.py \
	test/python/neatx.protocol_test.py \
	test/python/neatx.session_test.py \
	test/python/neatx.utils_test.py

nodist_TESTS =

TESTS = $(dist_TESTS) $(nodist_TESTS)

TESTS_ENVIRONMENT = PYTHONPATH=.:$(top_builddir)


all-local: stamp-directories lib/_autoconf.py extras/upload

clean-local:
	-test -z "$(CLEANDIRS)" || rm -rf $(CLEANDIRS)

extras/upload: extras/upload.in stamp-directories $(REPLACE_VARS_SED)
	sed -f $(REPLACE_VARS_SED) < $< > $@
	chmod u+x $@

$(TESTS): neatx srclinks lib/_autoconf.py src/fdcopy src/ttysetup

doc/%.html: doc/%.rst
	@if test -z "$(RST2HTML)"; then \
		echo 'rst2html not found during configure'; \
		exit 1; \
	fi
	$(RST2HTML) $< $@

lib/_autoconf.py: Makefile stamp-directories
	set -e; \
	{ echo '# This file is automatically generated, do not edit!'; \
	  echo '#'; \
	  echo ''; \
	  echo '"""Build-time configuration.'; \
	  echo '';\
	  echo 'This file is autogenerated by the build process.'; \
	  echo 'For any changes you need to re-run ./configure and'; \
	  echo 'not edit by hand.'; \
	  echo ''; \
	  echo '"""'; \
	  echo ''; \
	  echo "PACKAGE_VERSION = '$(PACKAGE_VERSION)'"; \
	  echo "VERSION_MAJOR = '$(VERSION_MAJOR)'"; \
	  echo "VERSION_MINOR = '$(VERSION_MINOR)'"; \
	  echo "VERSION_REVISION = '$(VERSION_REVISION)'"; \
	  echo "VERSION_SUFFIX = '$(VERSION_SUFFIX)'"; \
	  echo "VERSION_FULL = '$(VERSION_FULL)'"; \
	  echo "LOCALSTATEDIR = '$(localstatedir)'"; \
	  echo "SYSCONFDIR = '$(sysconfdir)'"; \
	  echo "PKGLIBDIR = '$(pkglibdir)'"; \
	} > $@

$(REPLACE_VARS_SED): Makefile stamp-directories
	set -e; \
	{ echo 's#@PREFIX@#$(prefix)#g'; \
	  echo 's#@SYSCONFDIR@#$(sysconfdir)#g'; \
	  echo 's#@LOCALSTATEDIR@#$(localstatedir)#g'; \
	  echo 's#@SBINDIR@#$(sbindir)#g'; \
	  echo 's#@PKGLIBDIR@#$(pkglibdir)#g'; \
	  echo 's#@PKGPYTHONDIR@#$(pkgpythondir)#g'; \
	} > $@

$(PYTHON_BOOTSTRAP): Makefile stamp-directories
	set -e; \
	module=`basename $@ | tr - _`; \
	{ echo "#!$(PYTHON)"; \
	  echo '# This file is automatically generated, do not edit!'; \
	  echo "from neatx.app import $$module"; \
	  echo "$$module.Main()"; \
	} > $@ || rm -f $@
	chmod u+x $@

$(LOG_WRAPPER): Makefile stamp-directories $(GEN_LOG_WRAPPER)
	set -e; \
	progname=`basename $@ | sed -e 's/-wrapper//'`; \
	$(GEN_LOG_WRAPPER) "$(pkglibdir)/$$progname" > $@ || rm -f $@
	chmod u+x $@

# We need to create symlinks because "make distcheck" will not install Python
# files when building.
srclinks: stamp-directories
	set -e; \
	for i in $(pkgpython_PYTHON) $(app_PYTHON) $(dist_pkglib_SCRIPTS); do \
		if test ! -f $$i -a -f $(abs_top_srcdir)/$$i; then \
			$(LN_S) $(abs_top_srcdir)/$$i $$i; \
		fi; \
	done

neatx:
	cd $(top_builddir) && test -h "$@" || { rm -f $@ && $(LN_S) lib $@; }

# a dist hook rule for catching revision control directories
distcheck-hook:
	if find $(top_distdir) | grep -F -e .svn -e .git; then \
		echo "Found revision control files in final archive" 1>&2 ; \
		exit 1; \
	fi

install-exec-local:
	@mkdir_p@ "$(DESTDIR)${localstatedir}/lib/neatx" \
	  "$(DESTDIR)${localstatedir}/lib/neatx/sessions"

stamp-directories: Makefile
	@mkdir_p@ $(DIRS)
	touch $@

.PHONY: apidoc
apidoc: all
	mkdir -p doc/api
	TMPDIR=`mktemp -d ` && { \
		cp -r lib $$TMPDIR && \
		( \
			CDIR=`pwd` && \
			cd $$TMPDIR && \
			mv lib neatx && \
			epydoc -v --conf $$CDIR/epydoc.conf -o $$CDIR/doc/api \
		); \
		rm -rf $$TMPDIR ; \
	}

# vim: set noet :
